package com.amway.qy.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amway.qy.core.constants.QyWechatUrls;
import com.amway.qy.core.service.QyAccessTokenService;
import com.amway.qy.core.service.QyAsyncTaskService;
import com.amway.qy.resource.result.async.QyAsyncJobTicketResult;
import com.amway.qy.util.common.QyHttpClientUtils;
import com.google.gson.Gson;

public class QyAsyncTaskServiceImpl implements QyAsyncTaskService {
	
	private static final Logger logger = LoggerFactory.getLogger(QyAsyncTaskServiceImpl.class);
	
	private QyAccessTokenService accessTokenManager;
	
	// Construct here
	public QyAsyncTaskServiceImpl(QyAccessTokenService accessTokenManager) {
		this.accessTokenManager = accessTokenManager;
	}
	 

	@Override
	public QyAsyncJobTicketResult updateMemberByIncrement(String media_id, String url, String token, String encodingaeskey) {
		logger.debug("updateMemberByIncrement-> URL:"+url+"  token:"+token+"  AES key:"+encodingaeskey+"  media_id:"+media_id);
		
		AsyncJobReq reqJson = null;
		
		if(url == null || token == null || encodingaeskey == null){
			reqJson = new AsyncJobReq(media_id,null);
		}else{
			AsyncJobCallback callback = new AsyncJobCallback(url,token,encodingaeskey);		
			reqJson = new AsyncJobReq(media_id,callback);
		}
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
		String ret = QyHttpClientUtils.httpsPostJson(QyWechatUrls.BATCH_UPDATE_MEMBER_INCREMENT, paramsMap, new Gson().toJson(reqJson));
		return new Gson().fromJson(ret, QyAsyncJobTicketResult.class);
	}

	@Override
	public QyAsyncJobTicketResult updateMemberByIncrement(String media_id) {
		return updateMemberByIncrement(media_id,null,null,null);
	}

	@Override
	public QyAsyncJobTicketResult overwriteMemberFully(String media_id, String url, String token, String encodingaeskey) {
		logger.debug("overwriteMemberFully-> URL:"+url+"  token:"+token+"  AES key:"+encodingaeskey+"  media_id:"+media_id);
		
		AsyncJobReq reqJson = null;
		
		if(url == null || token == null || encodingaeskey == null){
			reqJson = new AsyncJobReq(media_id,null);
		}else{
			AsyncJobCallback callback = new AsyncJobCallback(url,token,encodingaeskey);		
			reqJson = new AsyncJobReq(media_id,callback);
		}
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
		String ret = QyHttpClientUtils.httpsPostJson(QyWechatUrls.OVERWRITE_MEMBER_TOTALLY, paramsMap, new Gson().toJson(reqJson));
		return new Gson().fromJson(ret, QyAsyncJobTicketResult.class);
	}

	@Override
	public QyAsyncJobTicketResult overwriteMemberFully(String media_id) {
		return overwriteMemberFully(media_id,null,null,null);
	}

	@Override
	public QyAsyncJobTicketResult overwriteDepartmentFully(String media_id, String url, String token,
			String encodingaeskey) {
		logger.debug("overwriteDepartmentFully-> URL:"+url+"  token:"+token+"  AES key:"+encodingaeskey+"  media_id:"+media_id);
		
		AsyncJobReq reqJson = null;
		
		if(url == null || token == null || encodingaeskey == null){
			reqJson = new AsyncJobReq(media_id,null);
		}else{
			AsyncJobCallback callback = new AsyncJobCallback(url,token,encodingaeskey);		
			reqJson = new AsyncJobReq(media_id,callback);
		}
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
		String ret = QyHttpClientUtils.httpsPostJson(QyWechatUrls.OVERWRITE_DEPARTYMENT_TOTALLY, paramsMap, new Gson().toJson(reqJson));
		return new Gson().fromJson(ret, QyAsyncJobTicketResult.class);		
	}

	@Override
	public QyAsyncJobTicketResult overwriteDepartmentFully(String media_id) {
		return overwriteDepartmentFully(media_id,null,null,null);
	}

	@Override
	public QyAccessTokenService getQyAccessTokenService() {
		return this.accessTokenManager;
	}

	@Override
	public void setQyAccessTokenService(QyAccessTokenService tokenService) {
		this.accessTokenManager = tokenService;
	}
	
	
	
	/*
	 * 构造相应的Java Bean to cater for the request format as bellow:
	 * 
		 {
			"media_id":"xxxxxx",
			"callback":
			{
			 	"url": "xxx",
			 	"token": "xxx",
			 	"encodingaeskey": "xxx"
			}
		}
	 */
	
	private static class AsyncJobReq{
		private String media_id;
		private AsyncJobCallback callback;

		public AsyncJobReq(String media_id, AsyncJobCallback callback) {
			this.media_id = media_id;
			this.callback = callback;
		}
		public String getMedia_id() {
			return media_id;
		}
		public void setMedia_id(String media_id) {
			this.media_id = media_id;
		}
		public AsyncJobCallback getCallback() {
			return callback;
		}
		public void setCallback(AsyncJobCallback callback) {
			this.callback = callback;
		}
		
	}
	
	private static class AsyncJobCallback{
		private String url;
		private String token;
		private String encodingaeskey;
		public AsyncJobCallback(String url, String token, String encodingaeskey) {
			this.url = url;
			this.token = token;
			this.encodingaeskey = encodingaeskey;
		}
		public String getUrl() {
			return url;
		}
		public void setUrl(String url) {
			this.url = url;
		}
		public String getToken() {
			return token;
		}
		public void setToken(String token) {
			this.token = token;
		}
		public String getEncodingaeskey() {
			return encodingaeskey;
		}
		public void setEncodingaeskey(String encodingaeskey) {
			this.encodingaeskey = encodingaeskey;
		}
		
	}

	
}
